package org.springblade.modules.restaurant.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.restaurant.entity.CommoditySummary;
import org.springblade.modules.restaurant.service.ICommoditySummaryService;
import org.springblade.modules.restaurant.vo.CommoditySummaryVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author harry
 * @公众号 Harry技术
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/restaurant/commoditysummary")
@Tag(name = "API - 到货汇总")
public class ApiCommoditySummaryController {

	private ICommoditySummaryService commoditySummaryService;

	/**
	 * 分页 货物采购汇总表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入commoditySummary")
	public R<IPage<CommoditySummary>> list(CommoditySummaryVO commoditySummary, Query query) {
		LambdaQueryWrapper<CommoditySummary> wrapper = customizeQueryWrapper(commoditySummary);
		IPage<CommoditySummary> pages = commoditySummaryService.page(Condition.getPage(query), wrapper);
		return R.data(pages);
	}

	private LambdaQueryWrapper<CommoditySummary> customizeQueryWrapper(CommoditySummaryVO commoditySummary) {

		String[] searchTime = commoditySummary.getSearchTime();
		DateTime now = DateUtil.date();
		// 获取今天开始时间
		String startTime = DateUtil.formatDate(now);
		// 获取当前时间
		String endTime = DateUtil.formatDate(now);

		if (searchTime != null && searchTime.length == 2) {
			startTime = searchTime[0];
			endTime = searchTime[1];
		}
		if (StrUtil.isNotBlank(commoditySummary.getStartTime()) && StrUtil.isNotBlank(commoditySummary.getEndTime())) {
			startTime = commoditySummary.getStartTime();
			endTime = commoditySummary.getEndTime();
		}

		return Wrappers.<CommoditySummary>lambdaQuery()
			.eq(ObjectUtil.isNotNull(commoditySummary.getCategoryId()), CommoditySummary::getCategoryId, commoditySummary.getCategoryId())
			.like(StrUtil.isNotBlank(commoditySummary.getCommodityName()), CommoditySummary::getCommodityName, commoditySummary.getCommodityName())
			.between(CommoditySummary::getArrivalDate, startTime, endTime)
			.eq(ObjectUtil.isNotNull(commoditySummary.getStatus()), CommoditySummary::getStatus, commoditySummary.getStatus())
			.orderByDesc(CommoditySummary::getArrivalDate).orderByAsc(CommoditySummary::getSort)
			.orderByAsc(CommoditySummary::getId);
	}

}
